﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Office.Server.ApplicationRegistry.MetadataModel;
using Microsoft.Office.Server.ApplicationRegistry.Infrastructure;
using Microsoft.Office.Server.ApplicationRegistry.Runtime;
using Microsoft.Office.Server.ApplicationRegistry.SystemSpecific.Db;


namespace WriteToTheBDC
{
    class Program
    {
        static void Main(string[] args)
        {
            string sspName = "SharedServices1";
            string lobSystemName = "AdventureWorksDW";
            string lobSystemInstanceName = "AdventureWorksDWInstance";
            string entityName = "DimCurrency";
            string methodInstanceName = "Insert_DimCurrency_Instance";

            // Connect to the BDC Metadata Instance
            SqlSessionProvider.Instance().SetSharedResourceProviderToUse(sspName);
            NamedLobSystemDictionary lobSystems = ApplicationRegistry.GetLobSystems();
            LobSystem lobSystem = lobSystems[lobSystemName];
            NamedLobSystemInstanceDictionary lobInstances = lobSystem.GetLobSystemInstances();
            LobSystemInstance lobInstance = lobInstances[lobSystemInstanceName];

            // Locate the method desired
            Entity entityObject = lobInstance.GetEntities()[entityName];
            NamedMethodInstanceDictionary methodInstances = entityObject.GetMethodInstances();
            MethodInstance methInstance = methodInstances[methodInstanceName];

            // Get the parameters to the method
            Object[] methodArgs = methInstance.GetMethod().CreateDefaultParameterInstances(methInstance);

            // RSD, Serbian Dinar
            methodArgs[0] = "RSD";
            methodArgs[1] = "Serbian Dinar";

            // Execute the method
            entityObject.Execute(methInstance, lobInstance, ref methodArgs);

            Console.WriteLine(String.Format("Method {0} Executed",methodInstanceName));


        }
    }
}
